﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ChatNamespace;
using ChatDAL;

namespace ChatService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ServiceChat" in code, svc and config file together.
    public class ServiceChat : IServiceChat
    {
        private Chat chat;

        public ServiceChat()
        {
            this.chat = new Chat();
        }

        public List<ChannelDTO> ListChannels()
        {
            List<Channel> channels = this.chat.ListAllChannels();
            List<ChannelDTO> channelsDTO = new List<ChannelDTO>();
            foreach (var item in channels)
            {
                channelsDTO.Add(ChannelDTO.ConvertFrom(item));
            }

            return channelsDTO;
        }

        public MessageDTO[] ShowMessages(MessagesContactor contractor)
        {
            string channel = contractor.Channel;
            int number = contractor.NumberOfMessagesToBeShown;

            Message[] messages = this.chat.ShowLastMessages(channel, number);

            MessageDTO[] messagesConverted = MessageDTO.ConvertAllFrom(messages);
            return messagesConverted;
        }

        public void PostMessage(MessageDTO message)
        {
            Message messageToPost = message.ConvertToMessage();
            this.chat.PostNewMessage(messageToPost);
        }

        public void ResetChannel(ChannelDTO channelDTO)
        {
            Channel channel = channelDTO.ConvertToChannel();
            this.chat.CreateNewChannel(channel);
        }
    }
}
